package com.schooleducation.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.schooleducation.entity.StuDepartment;
import com.schooleducation.entity.StuMajor;
import com.schooleducation.entity.User;
import com.schooleducation.entity.UserInfo;
import com.schooleducation.service.ServiceFactory;

import net.sf.ezmorph.object.DateMorpher;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.JSONUtils;

@Controller
@RequestMapping("/userInfo")
public class UserInfoController {

	@Resource(name = "serviceFac")
	private ServiceFactory serviceFac;
	
	/**
	 * 获取用户信息界面
	 * @param session
	 * @param model
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET)
	public String getUserInfoPage(HttpSession session, Model model) {
		User user = (User) session.getAttribute("user");
		//验证是否登陆
		if (user != null) {
			
			int departmentId;
			//获取系别列表
			List<StuDepartment> lots = serviceFac.getUserService().getStuDepartmentList();
			//获取用户详细信息
			UserInfo ui = serviceFac.getUserService().getUserInfoAndMajorByUserId(user.getId());
			//检查专业是否选择
			if(ui.getMajor() == null) {
				departmentId = lots.get(0).getId();
			} else {
				departmentId = ui.getMajor().getStuDepartment().getId();
			}
			
			//获取相应系别列表
			List<StuMajor> sms = serviceFac.getUserService().getStuMajorListByDepartment(departmentId);
			
			model.addAttribute("user", user);
			model.addAttribute("userInfo", ui);
			model.addAttribute("department", lots);
			model.addAttribute("major", sms);
			return "userInfo";
		}else{
			return "redirect:login";
		}
	}
	
	@RequestMapping(value = "logo", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String updateUserLogo(HttpSession session, String logo) {
		Map<String, Object> state = new HashMap<String, Object>();
		User user = (User) session.getAttribute("user");
		//检查是否登陆
		if(user != null) {
			state.put("isLogin", "yes");
			user.setLogo(logo);
			if(serviceFac.getUserService().updateUserLogo(user)) {
				
				//更新session
				session.setAttribute("user", user);
				state.put("update", "success");
			} else {
				state.put("update", "fail");
			}
		} else {
			state.put("isLogin", "no");
		}
		return JSONObject.fromObject(state).toString();
	}
	
	/**
	 * 提交个人信息请求
	 * @param session
	 * @param data
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String updateUserInfo(HttpSession session, String data) {
		Map<String, Object> state = new HashMap<String, Object>();
		User user = (User) session.getAttribute("user");
		//检查是否登陆
		if(user != null) {
			state.put("isLogin", "yes");
			//json转Date的格式
			String[] dateFormats = new String[]{"yyyy-mm-dd"};
			//设置json转Date
			JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpher(dateFormats)); 
			User u = (User) JSONObject.toBean(JSONObject.fromObject(data), User.class);
			user.setPetName(u.getPetName());
			user.setUserInfo(u.getUserInfo());
			//更新个人信息
			if(serviceFac.getUserService().UpdateUserAndInfo(user)) {
				//更新session
				session.setAttribute("user", user);
				state.put("update", "success");
			} else {
				state.put("update", "fail");
			}
		} else {
			state.put("isLogin", "no");
		}
		return JSONObject.fromObject(state).toString();
	}
	
	/**
	 * 根据系别获取专业列表请求
	 * @param departmentId
	 * @return
	 */
	@RequestMapping(value = "major", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String getMajorByDepartment(int departmentId) {
		List<StuMajor> sms = serviceFac.getUserService().getStuMajorListByDepartment(departmentId);
		JsonConfig config = new JsonConfig();
		config.setExcludes(new String[]{"stuDepartment"});
		
		return JSONArray.fromObject(sms, config).toString();
	}
}
